package com.base.core.action.keywords;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import com.base.core.BaseAction;
import com.base.core.util.DateUtil;
import com.base.core.util.NetUtil;
import com.base.core.util.StringUtil;

@SuppressWarnings("serial")
public class BaiduIndexAction extends BaseAction{
	
	private String searchWord="";

	public String getSearchWord() {
		return searchWord;
	}

	public void setSearchWord(String searchWord) {
		this.searchWord = searchWord;
	}
	
	private List<baiduIndexInfo> infoList = new ArrayList<baiduIndexInfo>();
	
	public List<baiduIndexInfo> getInfoList() {
		return infoList;
	}

	public void setInfoList(List<baiduIndexInfo> infoList) {
		this.infoList = infoList;
	}

	public String execute(){
		if(!StringUtil.isEmpty(searchWord)){
		  if(searchWord.indexOf(",")!=-1 || searchWord.indexOf("，")!=-1){
			String[] words =searchWord.replaceAll("\\，", ",").split(",");
			for(String wd:words){
				if(!StringUtil.isEmpty(wd)){
					this.prepareData(wd);
				}
			}
		}else{
			this.prepareData(searchWord);
		}
		}
		return SUCCESS;
	}
	
	@SuppressWarnings("unused")
	private void prepareData(String wd){
		baiduIndexInfo bInfo = new baiduIndexInfo();
		String bdKey =getBaiduKey(wd);
		bInfo.setBdKey(bdKey);
		String startDate = DateUtil.getNextDate(new Date(), -7);
		bInfo.setStartDate(startDate);
		bInfo.setEndDate(DateUtil.getCurrDateStr());
		try {
			bInfo.setEncodeKey(URLEncoder.encode(wd,"utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		infoList.add(bInfo);
	}
	
	public String getBaiduKey(String skey){
	String keys ="";
	try {
		skey = URLEncoder.encode(skey,"gb2312");
		String result =new NetUtil().getTargetStr("http://index.baidu.com/main/word.php?word="+skey, "gb2312");
		Pattern pattern = Pattern.compile("(.*)eval\\(Dec\\((.*)\\)\\);\\s+var flashHot = baidu.swf.getMovie(.*)");
		Matcher mat = pattern.matcher(result);
		String key ="";
		if(mat.find()){
			key =mat.group(2);
		}
		String input ="";
		if(key.indexOf(",")!=-1){
			input =key.split(",")[0];
			keys =key.split(",")[1];
		}
		// 创建脚本引擎管理器
        ScriptEngineManager factory = new ScriptEngineManager();
        // 创建JavaScript引擎
        ScriptEngine engine = factory.getEngineByName("JavaScript");
        // 从字符串中赋值JavaScript脚本
        engine.eval(new java.io.FileReader(AmfTest.class.getClassLoader().getResource("").getPath()+ "myjs.js"));
        Invocable inv = (Invocable) engine;
        //inv.invokeFunction("Dec", "Hello", "Tony");

        
        //String str =String.valueOf(inv.invokeFunction("Dec", "20374121354520363b243030243636203a31203a32203535213734203432243030243636203a31203a3220353566646d61732c7275662f6774606975602d303d2235402034442330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d36472d32412235452036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036326f66243037243747203a31203a307175606a61456b676e787165732134372d33462d32412235452036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d3035223734203632716b657668243631203b40203a32203537303c36203430243043243442203840203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a356d626c636d70203435243141243636203a33363134203537213747203646243241243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243631736d73203a35203444213734203430382c302f3423373a24374b2735432034442137362733322533342337382437382737372036352137362733322533342337382437382737372036352137362733322533342337382437382737372036352137362733322533342337382437382737372036352137316773706f734963767b60626d273730203744213736273330506d6167766d243738666a706b686a65612330317668642134356664726d7171223734636864756a2430307168677c6d732b2d30372237472034412332402732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732367174692d33322d3144223734203637282c243046676867203a47517a676b63446a64687c7567732c73766223373a24374b2735432034442137362733322533342337382437382737372036352137362733322533342337382437382737372036352137362733322533342337382437382737372036352137362733322533342337382437382737372036352137316066416f6d6b74203a36203b43203535213736203431474446474240203a33203a41203741213545203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203435766f6f656123373f2436492737372036377077676c727261736168712d33372d30462235402034442330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330367461737723373f2436492735432034442137362733322533342337382437382737372036352137362733322533342337382437382737372036352137362733322533342337382437382737372036352137362733322533342337382437382737372036352137362733322533342337382437382737372036352137362733322533342337382437382732452034412135472733322533342337382437382737372036352137362733322533342337382437382737372036352137362733322533342337382437382737372036352137362733322533342337382437382737372036352137362733322533342337382437382737372036352137362733322533342337382437382737372036352137362733322533346d607172203b43203535213736207334346030247133663930207d34323e32217031676063243032243645203845203843203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734647467607125324523373824373a32203537213747203646243241243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31756d706c6861772037442330312735432134373a3134392f353628363d213245303133312c3434283a37203a30203241213747203646243241243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31756d706c68614a6469607527324325333423304a24373a6c706b69213736203346243043243442203840203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535607321364727333231243645203845203843203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a322035352137346b6527324325333436203a42203846203744213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a32203535213734203432243030243636203a31203a322035352137342034326663746473677c2d32442d30352237366d70717627324325334223374e686b6c677d2967656c607028616e6f25334261647c6472697b2b776d742036372330422730452136442d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d3035746b6575776d6976243141243636203a336d7c76752236452036432330476b6e65617e2b6a606c6c772b646a692036436b63686c253342756d67762b786a752237362036462332452730402134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d333563677c2236452036373530303a3638363f343c35353136356660362036372332452730402134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352335452730452136442d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342033412330422730452136442d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d30352237342036352330312732312134352d33352d3035223734203637606e607168426b68716d6f712d303722373d2037472120146581582","9668"));
		input =input.replaceAll("\\'", "");
		keys =keys.replaceAll("\\'", "");
        String str =String.valueOf(inv.invokeFunction("Dec",input,keys));
        str = str.replaceAll("\\s+", "");
        Pattern pat = Pattern.compile("(.*)key:\"(.*)\"}},\"flashContent\"\\);");
        Matcher t = pat.matcher(str);
        if(t.find()){
        	keys =t.group(2);
        }
	}catch(Exception e){
	    e.printStackTrace();	
	}
	    return keys;
	}

}

class baiduIndexInfo {
	private String word;
	private String bdKey;
	private String encodeKey;
	private String startDate;
	private String endDate;
	private String googleSL;
	public String getBdKey() {
		return bdKey;
	}
	public void setBdKey(String bdKey) {
		this.bdKey = bdKey;
	}
	public String getEncodeKey() {
		return encodeKey;
	}
	public void setEncodeKey(String encodeKey) {
		this.encodeKey = encodeKey;
	}
	public String getStartDate() {
		return startDate;
	}
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}
	public String getEndDate() {
		return endDate;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public String getWord() {
		return word;
	}
	public void setWord(String word) {
		this.word = word;
	}
	public String getGoogleSL() {
		return googleSL;
	}
	public void setGoogleSL(String googleSL) {
		this.googleSL = googleSL;
	}
}
